﻿using System.Text;
using ABCat.Core.Plugins.DataSets;
using Ionic.Zlib;
using SQLite.Net.Attributes;

namespace ABCat.Plugins.DataSources.AudioBooks
{
	[Table("BinaryData")]
	public class BinaryData : IBinaryData
	{
		[PrimaryKey]
		public string Key { get; set; }
		public bool IsCompressed { get; set; }
		[Column("Data")]
		public byte[] Data { get; set; }

		public string GetString()
		{
			if (IsCompressed) return Encoding.Default.GetString(GZipStream.UncompressBuffer(Data));
			return Encoding.Default.GetString(Data);
		}

		public byte[] GetData()
		{
			if (IsCompressed) return GZipStream.UncompressBuffer(Data);
			return Data;
		}

		public void SetString(string data, bool compress)
		{
			IsCompressed = compress;
			if (compress) Data = GZipStream.CompressBuffer(Encoding.Default.GetBytes(data));
			else Data = Encoding.Default.GetBytes(data);
		}

		public void SetData(byte[] data, bool compress)
		{
			IsCompressed = compress;
			if (compress) Data = GZipStream.CompressBuffer(data);
			else Data = data;
		}
	}
}